/**
 * 
 */
package com.base.myproject.client.ui;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.myproject.client.listener.HeardBeatEvent;
import com.base.myproject.client.listener.HeardBeatListener;
import com.base.myproject.client.tools.SendServlet;
import com.extjs.gxt.ui.client.event.Listener;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Event;


/**
 * 网页心跳
 * @author zgw
 *2009-7-7 上午11:29:15
 * javazgw@gmail.com
 */
public  class BaseHeartBeat  implements HeardBeatListener{
	 Map<String, List> listeners =new HashMap<String, List>();;
	static BaseHeartBeat bhb = null;
	 public  BaseHeartBeat() throws RequestException
	 {
		
		/* Button b = new Button("b");
		
		 b.addSelectionListener(new SelectionListener(){

			@Override
			public void componentSelected(ComponentEvent ce) {
				// TODO Auto-generated method stub
			//	getString();
			}
});
		 
//		 
//		 getString();
//		 onData();
//		 p_embed3123();
	
		 //this.setUrl("/test.html");
		 onData();
		 this.getElement().*/
		 
		final	SendServlet sendservlet = new SendServlet();
			RequestCallback rcb = new  RequestCallback(){

				@Override
				public void onError(Request request, Throwable exception) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onResponseReceived(Request request,
						Response response) {
					System.out.println("onResponseReceived"+response.getText());
					fireEvent(response.getText(),new HeardBeatEvent(this,response.getText()));
					try {
						sendservlet.sendServlet("HearBeatServletX", "Action=test", this);
					} catch (RequestException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			sendservlet.sendServlet("HearBeatServletX", "Action=test", rcb);
			this.addHeardBeatListener("zgw", this);
	 }
	static  public  BaseHeartBeat getinstance() 
	 {
		 if(bhb==null)
		 {
			 try {
				bhb = new BaseHeartBeat();
			} catch (RequestException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 return bhb;
			 
		 
	 }
	 public boolean fireEvent(String eventType, HeardBeatEvent be) {
			if (listeners != null) {

				List<HeardBeatListener> list = listeners.get(eventType);
				if (list != null) {
					for (HeardBeatListener l : list) {
					
						l.HeardBeatEventHandle(be);
					}
				}

			}
			return true;
		}

		public void addHeardBeatListener(String eventType, HeardBeatListener listener) {
			if (listener == null)
				return;
			if (listeners == null) {
				listeners = new HashMap<String, List>();
			}
			List<HeardBeatListener> list = listeners.get(eventType);
			if (list == null) {
				list = new ArrayList<HeardBeatListener>();
				listeners.put(eventType, list);
			}

			if (!list.contains(listener)) {
				list.add(listener);
			}

		}

		public void removeHeardBeatListener(final int eventType,
				final Listener listener) {
			if (listeners == null) {
				return;
			}

			List<Listener> list = listeners.get(eventType);
			if (list != null) {
				list.remove(listener);
				if (list.isEmpty()) {
					listeners.remove(eventType);
				}
			}

		}

	 public  native void getString() /*-{ 
	 //	$wnd.p_publish('/gwtbase/BaseServlet', 'action', 'test1', 'nick', 'dd', 'msg', 'zgw');
//	$wnd.PL.publish('pushlet.srv', 'p_event=join-listen&p_subject=/CometDemo/TestRate&p_format=xml-strict&p_mode=pull');
//	onData();

	function init() {
		$wnd.initDHTML();
		$wnd.p_join_listen('/stocks/aex');
		
	}
	init();

	// onData method called by pushlet frame


		 }-*/;
	 
	 public  native void onData() /*-{ 
	function onData(pushletEvent) {
		var valueLayer = $wnd.getRawObject($wnd.pushletEvent.get('name'));
		//$wnd.layerWrite(valueLayer, $wnd.pushletEvent.get('rate'));
		$wnd.alert("d");
		
	}
	 	
	
		 }-*/;
	 
	 public native void p_embed3123()/*-{
	 $wnd.p_embed();
	 }-*/;
	@Override
	public void HeardBeatEventHandle(HeardBeatEvent evt) {
		// TODO Auto-generated method stub
		System.out.println("eeeeee");
	}
	@Override
	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub
		
	}
	 
}
